package com.zhihaoliang.coroutine.adapter

import androidx.databinding.BindingAdapter
import android.widget.ImageView
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.Glide
import com.zhihaoliang.coroutine.R

object ImageViewAttrAdapter {

    @JvmStatic
    @BindingAdapter(value = ["imageUrl", "defaultId"], requireAll = false)
    fun loadImage(imageView: ImageView, url: String?, defaultId: Int?) {

        val defaultRes = defaultId ?: R.mipmap.ic_de_profile
        val options = RequestOptions().placeholder(defaultRes) //图片加载出来前，显示的图片
            .fallback(defaultRes) //url为空的时候,显示的图片
            .error(defaultRes)
        Glide.with(imageView.context).load(url).apply(options).into(imageView)
    }

}